package com.demo.apparel.mapper.web;

import com.demo.apparel.entity.Ana;
import com.demo.apparel.vo.web.AnaExportVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AnaMapper {

    @Select("SELECT * FROM ana ORDER BY created_time DESC")
    List<Ana> selectAll();

    @Insert("""
        INSERT INTO ana (id, text, created_time, updated_time)
        VALUES (#{id}, #{text}, #{createdTime}, #{updatedTime})
    """)
    void insert(Ana ana);

    @Delete("DELETE FROM ana WHERE id = #{id}")
    void deleteById(String id);

    @Select("""
        <script>
        SELECT * FROM ana
        <where>
            <if test="keyword != null and keyword != ''">
                AND text LIKE CONCAT('%', #{keyword}, '%')
            </if>
        </where>
        ORDER BY created_time DESC
        LIMIT #{pageSize} OFFSET #{offset}
        </script>
    """)
    List<Ana> selectPage(@Param("keyword") String keyword,
                         @Param("offset") int offset,
                         @Param("pageSize") int pageSize);

    @Select("""
        <script>
        SELECT COUNT(1) FROM ana
        <where>
            <if test="keyword != null and keyword != ''">
                AND text LIKE CONCAT('%', #{keyword}, '%')
            </if>
        </where>
        </script>
    """)
    long count(@Param("keyword") String keyword);

    @Delete("""
        <script>
        DELETE FROM ana WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        </script>
    """)
    void deleteBatch(@Param("ids") List<String> ids);

    @Select("SELECT text, created_time, updated_time FROM ana ORDER BY created_time DESC")
    List<AnaExportVO> getAllForExport();

    @Update("""
    UPDATE ana
    SET text = #{text},
        updated_time = NOW()
    WHERE id = #{id}
""")
    void updateById(Ana ana);


    @Insert("<script>" +
            "INSERT INTO ana (id, text, created_time, updated_time) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.id}, #{item.text}, #{item.createdTime}, #{item.updatedTime})" +
            "</foreach>" +
            "</script>")
    void insertBatch(@Param("list") List<Ana> anaList);
}